package com.ruoyi.employer.service.impl;

import java.util.List;

import com.ruoyi.employer.domain.PayWechat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.employer.mapper.PayAlipayMapper;
import com.ruoyi.employer.domain.PayAlipay;
import com.ruoyi.employer.service.IPayAlipayService;

/**
 * 货主支付宝钱包管理Service业务层处理
 * 
 * @author ysq
 * @date 2024-08-14
 */
@Service
public class PayAlipayServiceImpl implements IPayAlipayService 
{
    @Autowired
    private PayAlipayMapper payAlipayMapper;

    /**
     * 查询货主支付宝钱包管理
     * 
     * @param id 货主支付宝钱包管理主键
     * @return 货主支付宝钱包管理
     */
    @Override
    public PayAlipay selectPayAlipayById(Long id)
    {
        return payAlipayMapper.selectPayAlipayById(id);
    }

    /**
     * 查询货主支付宝钱包管理列表
     * 
     * @param payAlipay 货主支付宝钱包管理
     * @return 货主支付宝钱包管理
     */
    @Override
    public List<PayAlipay> selectPayAlipayList(PayAlipay payAlipay)
    {
        return payAlipayMapper.selectPayAlipayList(payAlipay);
    }

    /**
     * 新增货主支付宝钱包管理
     * 
     * @param payAlipay 货主支付宝钱包管理
     * @return 结果
     */
    @Override
    public int insertPayAlipay(PayAlipay payAlipay)
    {
        return payAlipayMapper.insertPayAlipay(payAlipay);
    }

    /**
     * 修改货主支付宝钱包管理
     * 
     * @param payAlipay 货主支付宝钱包管理
     * @return 结果
     */
    @Override
    public int updatePayAlipay(PayAlipay payAlipay)
    {
        return payAlipayMapper.updatePayAlipay(payAlipay);
    }

    /**
     * 批量删除货主支付宝钱包管理
     * 
     * @param ids 需要删除的货主支付宝钱包管理主键
     * @return 结果
     */
    @Override
    public int deletePayAlipayByIds(Long[] ids)
    {
        return payAlipayMapper.deletePayAlipayByIds(ids);
    }

    /**
     * 删除货主支付宝钱包管理信息
     * 
     * @param id 货主支付宝钱包管理主键
     * @return 结果
     */
    @Override
    public int deletePayAlipayById(Long id)
    {
        return payAlipayMapper.deletePayAlipayById(id);
    }
    /**
     * 充值操作
     *
     * @param id 用户ID
     * @param amount 充值金额
     * @return 更新是否成功
     */
    @Override
    public boolean recharge(Long id, double amount) {
        PayAlipay payAlipay = payAlipayMapper.selectById(id);
        if (payAlipay == null) {
            return false; // 用户不存在
        }
        //计算新的金额
        Double newBalance = payAlipay.getAlipayWallet() + amount;
        payAlipay.setAlipayWallet(newBalance);
        int rows = payAlipayMapper.updatePayAlipay(payAlipay);
        return rows > 0;
    }
    /**
     * 提现操作
     *
     * @param id 用户ID
     * @param amount 提现金额，购买商品
     * @return 更新是否成功
     */
    @Override
    public boolean withdraw(Long id, double amount) {
        // 从数据库获取 PayWechat 对象
        PayAlipay payAlipay = payAlipayMapper.selectById(id);
        if (payAlipay == null) {
            return false; // 用户不存在
        }

        // 获取钱包金额并进行计算
        Double currentBalance = payAlipay.getAlipayWallet();
        if (currentBalance == null) {
            currentBalance = 0.0; // 如果钱包金额为 null，则将其视为 0
        }

        // 计算新的金额
        Double newBalance = currentBalance - amount;
        if (newBalance < 0) {
            return false; // 余额不足
        }

        payAlipay.setAlipayWallet(newBalance);
        int rows = payAlipayMapper.updatePayAlipay(payAlipay);
        return rows > 0;
    }
}
